import wxParse from '../../wxParse/wxParse.js';
const App = getApp();

Page({

  data: {
    // 弹窗开关
    showModal:false,
    // 页面参数
    options: {},
    // 页面元素
    items: {},
    scrollTop: 0,

    doctorList: [],
    hospitalList: []
  },

  /* 生命周期函数--监听页面加载 */
  onLoad: function (options) {
    // 当前页面参数
    this.setData({
      options
    });
    // 加载页面数据
    this.getPageData();

    let app = this
    wx.getPrivacySetting({
      success: res => {
        console.log(res) // 返回结果为: res = { needAuthorization: true/false, privacyContractName: '《xxx隐私保护指引》' }
        if (res.needAuthorization) {
          // 需要弹出隐私协议
          app.setData({
            showModal: true
          })
        }
      },
      fail: (err) => {console.log(err)},
    })
  },

  openLocation(e){
    wx.openLocation({
      latitude: parseFloat(e.currentTarget.dataset.latitude),
      longitude: parseFloat(e.currentTarget.dataset.longitude),
      success: function() {
        console.log('success');
      },
      fail: function(err) {
        console.log(err)
      }
    });
  },
  
  // 用户隐私关闭弹出层
  go() {
    this.setData({
      showModal: false,
    })
  },
  
  handleAgreePrivacyAuthorization(res) {
    this.setData({
      showModal: false
    })
  },

  handleOpenPrivacyContract() {
    // 打开隐私协议页面
    wx.openPrivacyContract({
      success: () => {},
      fail: () => {}
    })
  },

  dissagree(){
    this.setData({
      showModal: false
    })
  },

  // 专家详情页面跳转
  doctorPage(e) {
    wx.navigateTo({
      url: '/pages/doctorPage/doctorPage?id=' + e.currentTarget.dataset.id,
    })
  },
  // 专家团队查看更多
  detailsPage() {
    wx.navigateTo({
      url: '/pages/doctorPage/detailsPage/detailsPage',
    })
  },

  // 机构详情页面跳转
  organPage() {
    wx.navigateTo({
      url: '/pages/organPage/organPage',
    })
  },
   // 合作机构详情页面跳转
   organDes(e) {
    wx.navigateTo({
      url: '/pages/organPage/organDes/organDes?id='+e.currentTarget.dataset.id,
    })
  },
  
  detailsPageTwo() {
    wx.switchTab({
      url: '/pages/organPage/detailsPage/detailsPage',
    })
  },

  // 要拨打的电话号码
  makePhoneCall(e) {
    const phoneNumber = e.currentTarget.dataset.type
    wx.makePhoneCall({
      phoneNumber: phoneNumber
    })
  },

  /**
   * 加载页面数据
   */
  getPageData: function (callback) {
    let _this = this;
    App._get('page/index', {
      page_id: _this.data.options.page_id || 0
    }, function (result) {
      // 设置顶部导航栏栏
      _this.setPageBar(result.data.page);
      _this.setData(result.data);
      _this.setData({
        doctorList: result.data.doctor_list,
        hospitalList: result.data.hospital_list
      })
      let content = result.data.shop_detail.summary
      // 富文本转码
      if (content.length > 0) {
        wxParse.wxParse('content', 'html', content, _this, 0);
      }
      // 回调函数
      typeof callback === 'function' && callback();
    });
  },

  /**
   * 设置顶部导航栏
   */
  setPageBar: function (page) {
    // 设置页面标题
    wx.setNavigationBarTitle({
      title: page.params.title
    });
    // 设置navbar标题、颜色
    wx.setNavigationBarColor({
      frontColor: page.style.titleTextColor === 'white' ? '#ffffff' : '#000000',
      backgroundColor: page.style.titleBackgroundColor
    })
  },

  /**
   * 分享当前页面
   */
  onShareAppMessage() {
    let _this = this;
    return {
      title: _this.data.page.params.share_title,
      path: "/pages/index/index?" + App.getShareUrlParams()
    };
  },

  /**
   * 下拉刷新
   */
  onPullDownRefresh: function () {
    // 获取首页数据
    this.getPageData(function () {
      wx.stopPullDownRefresh();
    });
  }

  // /**
  //  * 返回顶部
  //  */
  // goTop: function(t) {
  //   this.setData({
  //     scrollTop: 0
  //   });
  // },

  // scroll: function(t) {
  //   this.setData({
  //     indexSearch: t.detail.scrollTop
  //   }), t.detail.scrollTop > 300 ? this.setData({
  //     floorstatus: !0
  //   }) : this.setData({
  //     floorstatus: !1
  //   });
  // },

});